<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
      <el-form-item label="年度" prop="syYear">
        <el-date-picker v-model="queryParams.year" align="left" type="year" placeholder="选择年度"
            value-format="yyyy" format="yyyy" size="small" >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="水管机构" prop="orgNm" width="50px" class="qitem">
        <treeselect
            v-model="queryParams.orgCd"
            :options="xzqhOptions"
            :normalizer="normalizer"
            :show-count="true"
            placeholder="乡镇"
            @select="selectItem" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <div class="btcss" >{{ btallName }}水费收纳情况</div>
    <el-tabs type="border-card" >
      <el-tab-pane label="水费收纳数据" style="padding-bottom: 40px">
        <el-table show-summary v-loading="loading" :data="WrPriceList" :header-cell-style="{'text-align':'center'}" border >
            <el-table-column label="农户" align="left" prop="farmNm" />
            <el-table-column label="应收水费" align="right" prop="totalFee" />
            <el-table-column label="已缴金额" align="right" prop="payFee" />
            <el-table-column label="尚欠金额" align="right"  prop="saveFee" />
            <el-table-column label="完成（%）" align="right" prop="completeRate" >

            </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="水费收纳分析图表">
        <div id="sfshchars" ></div>
      </el-tab-pane>
    </el-tabs>



  </div>
</template>

<script>
import { orgFeeIncomeAnalys } from "@/api/waterPrice/payableContrast";
import { organtreeList } from "@/api/common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import * as echarts from "echarts";
export default {
  name: "WrPrice",
  components: { Treeselect },
  dicts: ['wr_month'],
  data() {
    return {
      // 行政区划树选项
      xzqhOptions: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水价设定表格数据
      WrPriceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        year: null,
        orgCd: 'ORG6543220000',
        adGrade: '4',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      btname: "",
      btallName: "",
      adGrad: "4"
    };
  },
  created() {
    var dt = new Date();
    var year = dt.getFullYear()
    this.queryParams.year = year+"";
    this.btname = "库额尔齐斯镇水管所"
    this.getTreeselect();
    this.getList();
  },
  mounted() {
    var wd = document.body.clientWidth * 0.8;
    document.getElementById("sfshchars").style.width = wd + "px";
  },
  methods: {
    dbs_echarts(jg,ys,ss,sq,bfb) {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById("sfshchars"));
        // 绘制图表
        myChart.setOption({
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
                }
            },
            legend: {
                data: ['应收', '实收', '尚欠','完成率']
            },
            xAxis: [
                {
                type: 'category',
                data: jg,
                axisPointer: {
                    type: 'shadow'
                }
                }
            ],
            yAxis: [
                {
                type: 'value',
                splitLine: { show: false },
                min: 0,
                axisLabel: {
                    formatter: '{value} 元'
                }
                },
                {
                type: 'value',
                splitLine: { show: false },
                min: 0,
                axisLabel: {
                    formatter: '{value} %'
                }
                }
            ],
            series: [
                {
                name: '应收',
                type: 'bar',
                barWidth: 25,
                tooltip: {
                    valueFormatter: function (value) {
                    return value + ' 元';
                    }
                },
                data: ys
                },
                {
                name: '实收',
                type: 'bar',
                barWidth: 25,
                tooltip: {
                    valueFormatter: function (value) {
                    return value + ' 元';
                    }
                },
                data: ss
                },
                {
                name: '尚欠',
                type: 'bar',
                barWidth: 25,
                tooltip: {
                    valueFormatter: function (value) {
                    return value + ' 元';
                    }
                },
                data: sq
                },
                {
                name: '完成率',
                type: 'line',
                yAxisIndex: 1,
                tooltip: {
                    valueFormatter: function (value) {
                    return value + ' %';
                    }
                },
                data: bfb
                }
            ]
            });
    },
    selectItem(e){
        this.adGrad = e.adGrad;
        this.btname = e.orgNm;
    },
    /** 查询水价设定列表 */
    getList() {
        this.loading = true;
        this.queryParams.adGrade = this.adGrad;
        orgFeeIncomeAnalys(this.queryParams).then(response => {
            this.btallName = this.queryParams.year+"年"+this.btname+"";
            let pd = response.data;
            this.WrPriceList = pd;
            let jg = [];
            let ys = [];
            let ss = [];
            let sq = [];
            let bfb = [];
            pd.forEach(pet => {
                jg.push(pet.orgNm?pet.orgNm:'');
                ys.push(pet.totalFee);
                ss.push(pet.payFee);
                sq.push(pet.saveFee)
                bfb.push(pet.completeRate);
            });
            this.dbs_echarts(jg,ys,ss,sq,bfb);
            this.loading = false;
        });
    },
    /** 查询行政区域下拉树结构 */
    getTreeselect() {
        organtreeList().then(response => {
            this.xzqhOptions = this.handleTree(response.data,"orgCd","parentOrgCd");
        });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
        if (node.children && !node.children.length) {
            delete node.children;
        }
        return {
            id: node.orgCd,
            label: node.orgNm,
            children: node.children
        };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },

  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
   .el-form-item--medium .el-form-item__content {
        line-height: 30px;
    }

    .vue-treeselect__placeholder, .vue-treeselect__single-value {
        line-height: 30px;
    }

    .vue-treeselect {
        width: 300px;
        height: 30px;
        .vue-treeselect__control {
            height: 30px;
            .el-form-item--medium .el-form-item__content {
                line-height: 30px;
                .vue-treeselect__placeholder, .vue-treeselect__single-value {
                    line-height: 30px;
                }
            }
        }
    }

    .btncss{
        width: 100%;
        margin-top:20px;
        text-align: right;
    }

    .btcss{
        width: 100%;
        text-align: center;
        font-size: 18px;
        font-weight: 600;
        margin: 20px 0;
    }

    #sfshchars{
        width: 100%;
        margin: 20px auto;
        height: 500px;
    }

</style>
